module.exports.screenDate = {
    init: function (options) {
        var me = this;
        me.options = {
            w: 0,
            time: new Date().getTime(),
            maxDay: 0,			// 可查询最大日期
            minDay: new Date(0)	// 可查询最小日期
        }
        for (var i in options) {
            me.options[i] = options[i];
        }

        var max = me.max();
        var thisTime = new Date();
        var star = 0;
        var end = parseInt((max.maxDay - me.options.time) / (1000 * 60 * 60 * 24));
        if (Math.abs(end) < 6) {
            star = end - 6;
        }
        var date_list = [];
        var idx = 0;
        for (var i = star; i <= end; i++) {
            if (idx >= 7) break;
            var times = me.get_time(me.options.time);
            times = me.get_time(times.date.setDate(times.date.getDate() + i));

            if (!me.max(times.date).star) {
                if (me.isToday(times.date, me.options.time)) {
                    times.class = 'active';
                }
                times.time = me.outputDate(times.date);
                times.left = idx * me.options.w;
                date_list.push(times);
            }
            idx += 1;
        }
        return date_list;
    },
    get_time: function (time) {
        var me = this;
        var date = new Date(time);
        var weekArr = {
            0: '星期天',
            1: '星期一',
            2: '星期二',
            3: '星期三',
            4: '星期四',
            5: '星期五',
            6: '星期六'
        }
        var weeks = weekArr[date.getDay()];
        var timeArr = {
            date: date,
            year: date.getFullYear(),
            month: date.getMonth() + 1,
            day: date.getDate(),
            weeks: weeks,
            class: ''
        }
        var star = timeArr.date;
        var end = new Date();
        if (me.isToday(star, end) == true) {
            timeArr.weeks = '今天';
        }
        return timeArr;
    },
    // 日期最大值
    max: function (time) {
        var me = this;
        var thisDay = new Date();
        var maxDay = new Date(thisDay.setDate(thisDay.getDate() + me.options.maxDay));
        var dB = new Date(time);
        var star = (dB.setHours(0, 0, 0, 0) > maxDay.setHours(0, 0, 0, 0));// 大于最大日期
        return {
            star: star,
            maxDay: maxDay
        }
    },
    // 日期最小值
    min: function (time) {
        var me = this;
        var thisDay = new Date();
        var minDay = (me.options.minDay instanceof Date ? me.options.minDay : new Date(thisDay.setDate(thisDay.getDate() - me.options.minDay)));
        var dB = new Date(time);
        var star = false;
        if (dB.setHours(0, 0, 0, 0) < minDay.setHours(0, 0, 0, 0)) {
            // 小于最小日期
            star = true;
        }
        return {
            star: star,
            minDay: minDay
        }
    },
    isToday: function (star, end) {
        if (new Date(star).setHours(0, 0, 0, 0) == new Date(end).setHours(0, 0, 0, 0)) {
            return true;
        } else {
            return false;
        }
    },
    /**
     * 字符串转日期
     */
    strforDate: function (str) {
        if (str.length != 8) {
            console.error('格式不正确：如19900101');
            return new Date();
        } else {
            return new Date(str.slice(0, 4) + "/" + str.slice(4, 6) + "/" + str.slice(6, 8));
        }
    },
    /**
     * 输出8位日期
     */
    outputDate: function (date) {
        var date = new Date(date);
        if (date == NaN) {
            console.error('传参格式不正确，必须是日期格式');
            date = new Date();
        }
        var month = date.getMonth() + 1,
            day = date.getDate();
        month = month < 10 ? "0" + month : month;
        day = day < 10 ? "0" + day : day;
        return date.getFullYear() + "" + month + "" + day;
    }
};
